import 'package:flutter/material.dart';
import 'package:xws/utils/app_util.dart';
import 'package:xws/utils/theme_util.dart';
import 'package:xws/widgets/app_btn.dart';

///选项底部弹框
class OptionsSheet extends StatelessWidget {
  const OptionsSheet({
    Key? key,
    this.child,
    this.padding,
    this.title,
    this.backgroundColor,
    this.options = const <String>[],
  }) : super(key: key);

  final Widget? child;
  final EdgeInsetsGeometry? padding;
  final String? title;
  final Color? backgroundColor;
  final List<String> options;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(20),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          _buildBox(context),
          const SizedBox(height: 10),
          _buildBtns(context),
        ],
      ),
    );
  }

  ///构建内容块
  Widget _buildBox(BuildContext context) {
    return Container(
      child: Column(
        children: <Widget>[
          if (title != null) _buildTitle(context),
          const SizedBox(height: 20),
          child ?? _buildOptions(context),
        ],
      ),
      padding: padding,
      decoration: BoxDecoration(
        color: backgroundColor,
        borderRadius: BorderRadius.circular(10),
      ),
    );
  }

  ///构建标题
  Widget _buildTitle(BuildContext context) {
    return Text(
      title ?? '',
      style: th(context).textTheme.subtitle1,
    );
  }

  ///构建取消按钮
  Widget _buildBtns(BuildContext context) {
    return AppBtn.center(
      onTap: () => close(context),
      text: '取消',
      textStyle: TextStyle(
        fontSize: 18,
        fontWeight: FontWeight.w500,
        color: th(context).textTheme.headline2?.color,
      ),
      height: 56,
      radius: 10,
      color: white(context),
    );
  }

  Widget _buildOptions(BuildContext context) {
    return ListView(
      shrinkWrap: true,
      padding: EdgeInsets.zero,
      children: List<Widget>.generate(
        options.length,
        (int i) => Padding(
          padding: const EdgeInsets.only(top: 6),
          child: AppBtn.center(
            onTap: () => close(context, value: i),
            text: options[i],
            textStyle: TextStyle(
              fontSize: 18,
              fontWeight: FontWeight.w500,
              color: theme(context),
            ),
            height: 56,
            radius: 10,
            color: white(context),
          ),
        ),
      ),
    );
  }
}
